package erQiao.yfl.app.service.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by xiang.yu on 2017/3/24.
 */
public class CacheClusterClient {
    Logger logger = LoggerFactory.getLogger("CacheClusterClient");
    private JedisPool pool;
    private String ip;
    //    private String auth;
    private String maxTotal;
    private String maxIdle;
    private String maxWaitMillis;
    private String testOnBorrow;
    private String testOnReturn;
    private String port;


    public void afterPropertiesSet() {
        try {
            logger.info("------------- redis pool init start------------- ");

            // 创建jedis池配置实例
            JedisPoolConfig config = new JedisPoolConfig();

            // 设置池配置项值
            config.setTestWhileIdle(false);
            config.setMaxTotal(Integer.valueOf(maxTotal));
            config.setMaxIdle(Integer.valueOf(maxIdle));
            config.setMaxWaitMillis(Long.valueOf(maxWaitMillis));
            config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
            config.setTestOnReturn(Boolean.valueOf(testOnReturn));

            pool = new JedisPool(config, ip, Integer.valueOf(port));

            boolean connected = isConnected();
            if (!connected) {
                logger.error("redis 初始化出错 缓存服务器连接不上！ ");
                throw new Exception("IP:" + ip);
            }

            logger.info("------------- redis pool init end------------- ");

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Error("IP:" + ip, e);
        }
    }

    public boolean isConnected() {
        return getRedis().isConnected();
    }

    public void destory() {
        pool.destroy();
    }

    public Jedis getRedis() {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        return jedis;
    }

    public Jedis getRedis(int index) {
        Jedis jedis = pool.getResource();
        jedis.select(index);
        return jedis;
    }

    /**
     * 返还到连接池
     *
     * @param pool
     * @param redis
     */
    public static void returnResource(JedisPool pool, Jedis redis) {
        if (redis != null) {
            pool.returnResource(redis);
        }
    }

    /**
     * 获取数据
     *
     * @param key
     * @return
     */
    public String get(String key) {
        String value = null;

        Jedis jedis = null;
        try {
            jedis = getRedis();
            value = jedis.get(key);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }

        return value;
    }

    /**
     * set数据
     *
     * @param key
     * @return
     */
    public String set(String key, String value) {
        String result = null;

        Jedis jedis = null;
        try {
            jedis = getRedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            //释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }

        return result;
    }

    public void returnRedis(Jedis jedis) {
        pool.returnResource(jedis);
    }

    public void returnBrokeRedis(Jedis jedis) {
        pool.returnBrokenResource(jedis);
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTota(String maxTotal) {
        this.maxTotal = maxTotal;
    }

    public String getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(String maxIdle) {
        this.maxIdle = maxIdle;
    }

    public String getMaxWaitMillis() {
        return maxWaitMillis;
    }

    public void setMaxWaitMillis(String maxWaitMillis) {
        this.maxWaitMillis = maxWaitMillis;
    }

    public String getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(String testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public String getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(String testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public String getPort() {
        return port;
    }


    public void setPort(String port) {
        this.port = port;
    }
}
